﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace TrafficApp
{
    /// <summary>
    ///    Represents a grid with rows and columns
    /// </summary>
    class Grid
    {
        public const int CELL_SIZE = 180; // height and width
        private bool[,] grid;             // busy status of each cell 
        
        /// <summary>
        ///     Initializes a new instance of the TrafficApp.Grid class with specified coordinates
        /// </summary>
        /// <param name="x">Number of rows</param>
        /// <param name="y">Number of columns</param>
        public Grid(int x, int y)
        {
            this.grid = new bool[x, y];
        }

        /// <summary>
        /// Finds cells numbers
        /// </summary>
        /// <param name="location">X and Y coordinates</param>
        /// <returns>Cells Numbers</returns>
        public Point getCells(Point location)
        {
            int x = location.X / CELL_SIZE;
            int y = location.Y / CELL_SIZE;
            return new Point(x, y);
        }

        /// <summary>
        /// Finds cells numbers as Vector
        /// </summary>
        /// <param name="location">X and Y coordinates</param>
        /// <returns>Cells Numbers</returns>
        public Vector getCellVec(Vector location)
        {
            int x = location.Xint / CELL_SIZE;
            int y = location.Yint / CELL_SIZE;
            return new Vector(x, y);
        }

        /// <summary>
        /// Finds cell number
        /// </summary>
        /// <param name="x">x or y coordinate</param>
        /// <returns>Cell Nr</returns>
        public int getCell(int x)
        {
            return x / CELL_SIZE;    
        }
        public bool isOccupied(Point location)
        {
            Point cell = getCells(location);
            return grid[cell.X, cell.Y];
        }

        /// <summary>
        /// Takes any position of the working area (pbxMain) and returns a valid starting coordinates
        /// of the cell
        /// </summary>
        /// <param name="location">X and Y coordinates</param>
        /// <returns>Valid starting Point (X and Y coordinates) of a cell</returns>
        public Point relocate(Point location)
        {
            location = getCells(location);
            location.X *= CELL_SIZE;
            location.Y *= CELL_SIZE;
            return location;
        }

        /// <summary>
        /// Takes any position of the working area (pbxMain) as Vector and returns a valid starting coordinates
        /// of the cell
        /// </summary>
        /// <param name="location">X and Y coordinates</param>
        /// <returns>Valid starting Point (X and Y coordinates) of a cell</returns>
        public Vector relocate(Vector location)
        {
            location = getCellVec(location);
            location.X *= CELL_SIZE;
            location.Y *= CELL_SIZE;
            return location;
        }

        /// <summary>
        /// Takes any position of the working area (pbxMain) and returns a valid starting coordinates
        /// of the cell
        /// </summary>
        /// <param name="x">x or y coordinate</param>
        /// <returns>Valid starting coordinate of a cell</returns>
        public int relocate(int x)
        {
            return getCell(x) * CELL_SIZE;
        }
        
        public void occupyCell(Point location)
        {
            Point cell = getCells(location);
            grid[cell.X, cell.Y] = true;
        }
        public void freeCell(Point location)
        {
            Point cell = getCells(location);
            grid[cell.X, cell.Y] = false;
        }
    }
}
